/**
 * Created by 鄢鹏权 on 2017/06/29.
 */
declare let wx:any;
class Wechat{
    static WechatUser={};
    static Config={};
    static Callback={
        config:[],
        user:[],
        chooseImage:[],
        uploadImage:[],
    };
    static config=function(Server:string,AppID:string,WechatID:string,Debug:boolean=false,Location=''){
        if(wx){
            Castle.Request.post(
                'http://'+Server+'/w.php?i=Js/config',
                {
                    ID:WechatID,
                    APIs:[
                        'checkJsApi',
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'onMenuShareQQ',
                        'onMenuShareWeibo',
                        'hideMenuItems',
                        'showMenuItems',
                        'hideAllNonBaseMenuItem',
                        'showAllNonBaseMenuItem',
                        'translateVoice',
                        'startRecord',
                        'stopRecord',
                        'onRecordEnd',
                        'playVoice',
                        'pauseVoice',
                        'stopVoice',
                        'uploadVoice',
                        'downloadVoice',
                        'chooseImage',
                        'previewImage',
                        'uploadImage',
                        'downloadImage',
                        'getNetworkType',
                        'openLocation',
                        'getLocation',
                        'hideOptionMenu',
                        'showOptionMenu',
                        'closeWindow',
                        'scanQRCode',
                        'chooseWXPay',
                        'openProductSpecificView',
                        'addCard',
                        'chooseCard',
                        'openCard'
                    ],
                    Debug:Debug,
                    URL:Location?Location:window.location.href
                },
                function (data) {
                    Wechat.Config=data;
                    wx.config(data);
                    Wechat.fire('config',data);
                    Castle.Request.get('http://'+Server+'/w.php?i=Js/getLogined',function (user) {
                        if(false===user){
                            window.location.href='http://'+Server+'/w.php?i=Js/user&ID='+WechatID;
                        }else{
                            Wechat.fire('user',user);
                            Wechat.WechatUser=user;
                        }
                    })
                }
            );
        }else{
            alert('请使用微信打开链接');
        }
    };
    static upload=function (id,success:Function) {
          wx.uploadImage({
              localId:id,
              isShowProgressTips:1,
              success:function (res) {
                  let data ={
                      id:id,
                      url:res.serverId
                  };
                  Wechat.fire('uploadImage',data)
                  if(success instanceof Function){
                      success.apply(this,[data])
                  }
              }
          })
    };
    static previewImage(Current,URLs:any=[]){
        if(URLs.length<1){
            URLs.push(Current)
        }
        if(URLs.indexOf(Current)==-1){
            URLs.push(Current)
        }
        wx.previewImage({
            current:Current,
            urls:URLs,
            success:function(){
                System.success('success')
            },
            error:function (e) {
                System.log(e)
            }
        })
    }
    static chooseImage=function (Count=1,SizeType=['original','compressed'],SourceType=['album','camera'],success:Function=null,error:Function=null) {
        wx.chooseImage({
            count: Count, // 默认9
            sizeType: SizeType, // 可以指定是原图还是压缩图，默认二者都有
            sourceType: SourceType, // 可以指定来源是相册还是相机，默认二者都有
            success: function (res:any) {
                let data = {
                    Success:res.errMsg=='chooseImage:ok',
                    LocalIDs:res.localIds
                };
                Wechat.fire('ChooseImage',data)
                if(res.errMsg=='chooseImage:ok'){
                    if(success instanceof Function){
                        success.apply(this,[data])
                    }
                }else{
                    if(error instanceof Function){
                        error.apply(this,[data])
                    }
                }
            }
        });
    };
    /**
     * 获取GPS坐标
     */
    static getGPSLocation=function (success:Function) {
        wx.getLocation({
            type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
            success: function (res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                var speed = res.speed; // 速度，以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                if(success instanceof  Function){
                    success.apply(this,[{
                        lat:latitude,
                        lon:longitude,
                        speed:speed,
                        accuracy:accuracy
                    }])
                }
            },fail:function (e) {
                console.log(e)
            }
        });
    }
    /**
     * 火星坐标
     */
    static getTXLocation=function (success:Function) {
        wx.getLocation({
            type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
            success: function (res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                var speed = res.speed; // 速度，以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                if(success instanceof  Function){
                    success.call(this,[res.latitude,res.longitude,res.speed,res.accuracy])
                }
            },fail:function (e) {
                console.log(e)
            }
        });
    }
    /**
     * 监听回调
     * @param event
     * @param func
     */
    static on(event:string,func:Function){
        Wechat.Callback[event].push([this,func])
    };

    /**
     * 触发回调
     * @param event
     * @param data
     */
    static fire(event:string,data:any){
        if(Wechat.Callback[event] instanceof Array){
            for (let i = 0;i<Wechat.Callback[event].length;i++){
                if(Wechat.Callback[event][i][1] instanceof Function){
                    Wechat.Callback[event][i][1].apply(this,[data])
                }
            }
        }
    }
}